﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Shopsearch_result : System.Web.UI.Page
{
    private List<Shop> matchedShops;
    private List<Shop> filteredShops;

    string keywords;

    protected void Page_Load(object sender, EventArgs e)
    {
        keywords = Request.QueryString["q"];
        Label1.Text = keywords;
        searchBox.Keyword = keywords;

        GetSearchResult();
        GetFilteredResult();
    }


    private void GetSearchResult()
    {
        var connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"];
        if (connString == null)
            throw new ArgumentNullException("无法获取指定的数据库连接字符串。");

        using (SqlConnection connection = new SqlConnection(connString.ConnectionString))
        {
            connection.Open();

            SqlCommand command = new SqlCommand(
                string.Format("SELECT * FROM [RatedShop] WHERE ([name] LIKE '%{0}%')", keywords),
                connection);
            command.CommandType = System.Data.CommandType.Text;
            var reader = command.ExecuteReader();

            matchedShops = new List<Shop>();
            while (reader.Read())
            {
                matchedShops.Add(new Shop
                {
                    Name = reader.GetString(0),
                    Description = reader.GetString(1),
                    CoverPhoto = reader.IsDBNull(2) ? string.Empty : reader.GetString(2),
                    Category = reader.GetString(5),
                    Owner = reader.GetString(6)
                });
            }
        }
    }

    void GetFilteredResult()
    {
        Contract.Requires(matchedShops != null);

        if (shopCategoryList.SelectedIndex == -1)
        {
            filteredShops = matchedShops;
        }
        else
        {
            filteredShops = new List<Shop>();
            for (int i = 0; i < shopCategoryList.Items.Count; i++)
            {
                if (shopCategoryList.Items[i].Selected)
                {
                    var r = from shop in matchedShops
                            where shop.Category == shopCategoryList.Items[i].Text
                            select shop;
                    filteredShops.AddRange(r);
                }
            }
        }
    }

    protected void searchResultSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        e.Result = matchedShops;
    }


    protected void filterResultSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        e.Result = filteredShops;
    }

    public class Shop
    {
        public string Name { get; set; }

        public string Description { get; set; }

        public string CoverPhoto { get; set; }

        public string Category { get; set; }

        public string Owner { get; set; }
    }
}